package com.training.java.redbox.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.training.java.redbox.domain.Customer;
import com.training.java.redbox.domain.DAOException;
import com.training.java.redbox.domain.Orders;
import com.training.java.redbox.domain.OrdersDAO;

@Service
public class OrdersServiceImpl implements OrdersService {

	@Autowired
	private OrdersDAO dao;
	
	@Override
	public void submitOrder(List<Long> ids, Customer customer) throws ServiceException {
		
		long orderNum;
		try {
			
			orderNum = dao.createOrderNumber();
			
		} catch (DAOException e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
		for(long id : ids)
		{
		      Orders order = new Orders(orderNum, 0, customer.getCustomerId(), id, "Rented");
		     
		      try {
		    	  
				dao.createOrders(order);
				
			} catch (DAOException e) {
				e.printStackTrace();
				throw new ServiceException(e);
			}
		}
		
	}



}
